/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortus.util;

import ortus.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

import sagex.api.Database;
import sagex.api.MediaFileAPI;
import sagex.api.ShowAPI;
import java.io.File;
import sagex.UIContext;

/**
 *
 * @author Aaron
 */
public class dump extends ortusvars{
	public static String DumpObject(Object o1)
	{
		try {
			ortus.api.DebugLog(new UIContext("SAGETV_PROCESS_LOCAL_UI"), "DumpObject: " + o1);

		} catch (Exception e) {
		}
		return "a";
	}

	/**
	 * Dump all MediaFiles to the sage directory in TitleFile.dat file
	 * @return
	 */
	public String DumpMedia()
	{
		return this.DumpMedia("TitleFile.dat");
	}

	/**
	 * Dump all MediaFiles to a custom directory
	 * @param outFile Path to dump the media database
	 * @return
	 */
	public String DumpMedia(String outFile)
	{
		BufferedWriter out;
		int noTitles = 0;
		UIContext UIC = new UIContext("SAGETV_PROCESS_LOCAL_UI");
//		API sageapi = new API();
//		MediaFileAPI mf = new MediaFileAPI(sageapi);
//		MediaFileAPI.List mfl = mf.GetMediaFiles();

		Object[] mfl = sagex.api.MediaFileAPI.GetMediaFiles(UIC);

		noTitles = mfl.length;

		try {
			out = new BufferedWriter(new FileWriter(outFile));
		} catch (IOException e) {
			return "Failed to open file";
		}

		for (int x = 0; x < mfl.length; x++) {
			MediaFileAPI obj = (MediaFileAPI) mfl[x];
			try {
				out.write(MediaFileAPI.GetMediaTitle(UIC, obj) + "\r\n");
			} catch (IOException e) {
				return "Failed to write file";
			}
		}

		try {
			out.close();
		} catch (IOException e) {
			return "Failed to Close file";
		}

		return "Wrote " + noTitles + " to file " + outFile;

	}

	/**
	 * Dump all imported videos to a file TitleFile.dat under the sage directory
	 * @return
	 */
	public String DumpVideo()
	{
		return this.DumpVideo("TitleFile.dat");
	}

//		public void TestDirectory() {
//			import java.io.File;
//			import java.io.IOException;
//			public class FileUtil {
//			  public static void main(String[] a)throws IOException{
//			    showDir(1, new File("d:\\Java_Dev"));
//			  }
//			  static void showDir(int indent, File file) throws IOException {
//			    for (int i = 0; i < indent; i++)
//			      System.out.print('-');
//			    System.out.println(file.getName());
//			    if (file.isDirectory()) {
//			      File[] files = file.listFiles();
//			      for (int i = 0; i < files.length; i++)
//			        showDir(indent + 4, files[i]);
//			    }
//			  }
//			}
//		}
	/**
	 * Dump all imported videos to a custom file
	 * @param outFile File name to dump the imported list
	 * @return
	 */
	public String DumpVideo(String outFile)
	{
		BufferedWriter out;
		int noTitles = 0;
		UIContext UIC = new UIContext("SAGETV_PROCESS_LOCAL_UI");
		List<Object> mfl = new ArrayList<Object>(Arrays.asList(MediaFileAPI.GetMediaFiles(UIC)));
		Database.FilterByBoolMethod(UIC, mfl, "IsVideoFile|IsDVD", true);
		Database.FilterByBoolMethod(UIC, mfl, "IsLibraryFile", true);
		Database.FilterByBoolMethod(UIC, mfl, "IsTVFile", false);
		Database.SortLexical(UIC, mfl, false, "GetMediaTitle");
		noTitles = mfl.size();

		try {
			out = new BufferedWriter(new FileWriter(outFile));
		} catch (IOException e) {
			return "Failed to open file";
		}

		for (Iterator<Object> iter = mfl.iterator(); iter.hasNext();) {
			MediaFileAPI obj = (MediaFileAPI) iter.next();
//				AiringAPI.Airing a = obj.GetMediaFileAiring();
//				ShowAPI.Show s = a.GetShow();
			String title = ShowAPI.GetShowEpisode(UIC, obj);
			// String Desc = s.GetShowDescription();
			File mfile = MediaFileAPI.GetFileForSegment(UIC, obj, 0);
			String fileloc = mfile.getPath();
			String filetype;
			if (MediaFileAPI.IsDVD(UIC, obj)) {
				filetype = "DVD";
			} else {
				filetype = MediaFileAPI.GetMediaFileFormatDescription(UIC, obj);
			}

			// String fileloc = s.GetShowTitle();
			Long showlen = MediaFileAPI.GetFileDuration(UIC, obj);
			Long filesiz = MediaFileAPI.GetSize(UIC, obj);
			String outrec = title + ";" + filetype + ";" + fileloc + ";" + showlen.toString() + ";" + filesiz.toString() + "\r\n";
			try {
				out.write(outrec);
			} catch (IOException e) {
				return "Failed to write file";
			}
		}

		try {
			out.close();
		} catch (IOException e) {
			return "Failed to Close file";
		}

		return "Wrote " + noTitles + " to file " + outFile;

	}

	static public List<String> DumpLogFile(String logfile)
	{
		List<String> LogEntries = new ArrayList<String>();
		try {
			FileInputStream fstream = new FileInputStream(logfile);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				LogEntries.add(strLine);
			}
			in.close();

			return LogEntries;

		} catch (Exception e) {
			ortus.api.DebugLog(ERROR, "DumpLogFile: " + logfile + " not found");
			return LogEntries;
		}

	}
}
